package io.renren.modules.generator.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.generator.entity.*;
import io.renren.modules.generator.service.*;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.sound.midi.Soundbank;


/**
 * 简历分享记录表; InnoDB free: 10240 kB
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-07-14 13:56:25
 */
@RestController
@RequestMapping("generator/share")
public class ShareController extends AbstractController {
    @Autowired
    private ShareService shareService;

    @Autowired
    private FriendsService friendsService;

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private MyResumeService myResumeService;

//    @Autowired
//    private InterviewService interviewService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:share:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shareService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*
      朋友圈简历列表
     */
    @RequestMapping("/friendsShareList")
    public R friendsShareList(String resExpectedOccupation){
        List<Long> friIdList=friendsService.getReceiverList(getUserId());
//        List list1=friendsService.getSenderList(getUserId());
//        for (int i = 0; i <list1.size() ; i++) {
//            friIdList.add(list1.get(i));
//        }
        friIdList.addAll(friendsService.getSenderList(getUserId()));
//        System.out.println(friIdList);  //好友Ids (通过验证的)
//        Map<String,Object> params=new HashMap<>();
//        params.put("list",friIdList);
//        System.out.println(friIdList);
        List list=shareService.getList(friIdList,resExpectedOccupation);
//        System.out.println(list);

        return R.ok().put("list",list);
    }

    @RequestMapping("/friendsShareList2")
    public R friendsShareList2(){

        //获取验证通过好友：我作为发送者，我作为接受者，且状态为2的
        Long myId = getUserId();
        List<FriendsEntity> senderList = friendsService.list(new QueryWrapper<FriendsEntity>().eq("fri_sender",myId).eq("fri_state",2));
        List<FriendsEntity> receiverList = friendsService.list(new QueryWrapper<FriendsEntity>().eq("fri_receiver",myId).eq("fri_state",2));
        List<Long> friList = new ArrayList<>();

        for(FriendsEntity f : senderList){
            friList.add(f.getFriReceiver());

        }
        for(FriendsEntity f : receiverList){
            friList.add(f.getFriSender());
        }

        //从分享表中获取所有朋友的分享的简历列表
        List<ShareEntity> shareList = new ArrayList<>();

        shareList = shareService.list(new QueryWrapper<ShareEntity>().in("sha_hr",friList));

        List<Long> resIdList = new ArrayList<Long>();
        for(ShareEntity shareEntity : shareList){
            resIdList.add(shareEntity.getShaResid());
        }

        List<ResumeEntity> resumeList = resumeService.list(new QueryWrapper<ResumeEntity>().in("res_id",resIdList));





        return R.ok();
    }


    /*
    添加分享
     */
    @RequestMapping("/add")
    public R share(Long resId,Long userId){
//        System.out.println(resId+","+userId);
        ShareEntity share=new ShareEntity();
        share.setShaHr(userId);
        share.setShaResid(resId);
        share.setShaTime(new java.sql.Date(new java.util.Date().getTime()));
        List list=shareService.getResId();
        for (int i = 0; i <list.size() ; i++) {
            if(resId==list.get(i)){
                return R.error("该简历已经分享！");
            }
        }

        shareService.save(share);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{resId}")
    public R info(@PathVariable("resId") Long resId){
//        System.out.println(resId);
        List list = resumeService.list(new QueryWrapper<ResumeEntity>().eq("res_id",resId));
//        System.out.println(list);
        return R.ok().put("list",list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:share:save")
    public R save(@RequestBody ShareEntity share){
		shareService.save(share);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:share:update")
    public R update(@RequestBody ShareEntity share){
        shareService.updateById(share);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/downlodeByResId")
    public R downlodeByResId(Long resId ){
//        System.out.println(resId+","+preTime);
//        try{
//            preTime = preTime.replace("Z", " UTC");
//            SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
//            SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            java.util.Date d = utcFormat.parse(preTime);
//            preTime=defaultFormat.format(d);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        //根据当前用户ID查询myResume中的resId
        List<MyResumeEntity> myList=myResumeService.list(new QueryWrapper<MyResumeEntity>().eq("my_hr_id",getUserId()));
        for (MyResumeEntity myResumeEntity: myList ){
            if(resId==myResumeEntity.getMyResId()){
                return R.error("您已下载该简历，请勿重复下载！！！");
            }
        }
        MyResumeEntity myResume=new MyResumeEntity();
        myResume.setMyResId(resId);
        myResume.setMyHrId(getUserId());//得到操作者ID，表示这个简历以后自己可以操作
        myResume.setMyResFrom(2L);//朋友分享
        myResume.setMyTime(new java.sql.Date(new java.util.Date().getTime()));
        myResume.setMyResState(4L);//状态为未处理
        myResumeService.save(myResume);
//        InterviewEntity interview=new InterviewEntity();
//        interview.setIntwMobile(resMobile);
//        interview.setIntwName(resName);
//        interview.setIntwBookTime(preTime);
//        interview.setIntwResumeId(resId);
//        interview.setIntwHrId(getUserId());
//        interviewService.save(interview);



        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:share:delete")
    public R delete(@RequestBody Long[] shaIds){
		shareService.removeByIds(Arrays.asList(shaIds));

        return R.ok();
    }

}
